#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

void first_clean(void *arg)
{
    printf(" %s first clean \n", (char *)arg);
    // return (void *)1;
}

void second_clean(void *arg)
{
    printf(" %s second clean \n", (char *)arg);
    // return (void *)0;
}

void *thread_fun1(void *arg)
{
    printf(" new thread 1 \n");
    printf(" pthread_cleanup_push beforce \n");
    
    pthread_cleanup_push(first_clean, (void *)"thread1");
    pthread_cleanup_push(second_clean, (void *)"thread1");

    printf(" pthread_cleanup_push after \n");

    pthread_cleanup_pop(1);
    // pthread_exit((void*)1);
    pthread_cleanup_pop(0);
    // return (void *)1;
}

void *thread_fun2(void *arg)
{
    printf(" new thread 2 \n");
    pthread_cleanup_push(first_clean, (void *)"thread2");
    pthread_cleanup_push(second_clean, (void *)"thread2");

    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)1);
}

int main(int argc, char **argv)
{
    pthread_t tid1, tid2;
    int err;
    err = pthread_create(&tid1, NULL, thread_fun1, NULL);
    pthread_join(tid1,NULL);
    // sleep(1);
    return 0;
}